package com.intelligentpension.bleconnect

import android.annotation.SuppressLint
import android.bluetooth.BluetoothDevice
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.hjq.toast.ToastUtils

/**
 * 版权：点赞 版权所有
 * author :赵航
 * @e-mail: zhaohangcn@gmail.com
 * time：2022/4/8
 * describe ：
 */
class BLRReceiver(var mLesten: diveceListen?) : BroadcastReceiver() {
    interface diveceListen {
        fun found(dev: BluetoothDevice?)
    }

    open fun setLesten(lesten: diveceListen) {
        this.mLesten = lesten
    }

    @SuppressLint("MissingPermission")
    override fun onReceive(p0: Context?, p1: Intent?) {
        if (p1 == null) return
        var action = p1.action

        //正在发现蓝牙
        if (BluetoothDevice.ACTION_FOUND == action) {
            var dev = p1.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)
            if (mLesten != null) {
                mLesten!!.found(dev)
            }
        }

        //正在配对蓝牙
        if (BluetoothDevice.ACTION_BOND_STATE_CHANGED == action) {
            var device = p1.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)
            when (device?.bondState) {
                //正在配对
                BluetoothDevice.BOND_BONDING -> {
                    ToastUtils.show("正在配对")
                }
                BluetoothDevice.BOND_BONDED -> {
                    ToastUtils.show("已经完成配对")
                }
                BluetoothDevice.BOND_NONE -> {
                    ToastUtils.show("取消配对")
                }
            }

        }
    }

}